package com.kele.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@RequestMapping("download")
@CrossOrigin
public class DownloadController {

    @Autowired
    HttpServletResponse response;

    @GetMapping("")
    public void download() throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition",
                "attachment;filename=" + URLEncoder.encode("test.txt", "UTF-8"));
        // 控制输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //代码形成Excel，PDF，Image 形成字节
        /*File file = new File("d:\\01_1905A\\kele_mall\\images_1905A\\huawei_logo.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);

        byte[]tt=new byte[fileInputStream.available()];
        int z;
        while((z=fileInputStream.read(tt, 0, tt.length))!=-1){

        }*/
        outputStream.write("hello world 1905".getBytes());
        outputStream.close();
        /*
        PrintWriter printWriter = new PrintWriter(outputStream);
        IOUtils.write("hello 1905", printWriter);*/
    }
}
